package socket

import (
	"common/comCopier"
	"common/dao/model"
	"common/response"
	"common/utils"
	"fmt"
	"gorm.io/gorm"
	"main/types"
	"time"
)

// ProcessMessages 处理nats消息 发送给接受者
func ProcessMessages(bt []byte) error {
	messageData, err := types.GetMessageData[model.Message](string(bt))
	if err != nil {
		return err
	}
	if messageData.GroupID != 0 {
		err := ProcessGroupMessages(messageData)
		if err != nil {
			return err
		}
		db := Manager.svc.DB
		table := db.Table(model.TableNameMessage)
		err = table.Transaction(func(tx *gorm.DB) error {
			if err := tx.Create(&messageData).Error; err != nil {
				return err
			}
			return nil
		})
		if err != nil {
			return err
		}
	}
	return nil
}
func ProcessGroupMessages(data model.Message) error {
	//data.GroupID
	var (
		members []model.GroupMember
		user    model.User
	)
	db := Manager.svc.DB
	table := db.Table(model.TableNameGroupMember)
	table1 := db.Table(model.TableNameUser)

	err := table1.Transaction(func(tx *gorm.DB) error {
		if err := tx.Where("uuid = ?", data.SenderUUID).Where("is_del = ?", 0).First(&user).Error; err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		return err
	}
	mess := types.ReceiveMessages{}
	err = comCopier.CopyWithOption(&mess, data)
	if err != nil {
		return err
	}
	mess.Picture = user.Picture
	mess.UserUuid = user.UUID
	mess.NickName = user.NickName
	resp := types.Response{
		Type: types.SysHi,
		Body: response.Body{
			Code:    1,
			Data:    mess,
			Message: "成功",
		},
	}
	err = table.Transaction(func(tx *gorm.DB) error {
		if err := tx.Where("group_id = ? ", data.GroupID).Where("left_at IS NULL").Find(&members).Error; err != nil {
			return err
		}
		if len(members) != 0 {
			Accounts := make([]string, 0)
			// 获取账号的uuid 并切去重
			for _, member := range members {
				b := utils.Contains(Accounts, member.UserUUID)
				if !b {
					Accounts = append(Accounts, member.UserUUID)
				}

			}
			message, err := GetWebSocketMessage(&resp)
			if err != nil {
				sendErrByAccounts(Accounts, "", err)
			}
			SendByAccounts(Accounts, message)
		}
		return nil
	})
	if err != nil {
		return err
	}
	return nil
}
func NotificationMessage(data model.GroupMember) error {
	var (
		members []model.GroupMember
		user    model.User
	)
	db := Manager.svc.DB
	table := db.Table(model.TableNameGroupMember)
	table1 := db.Table(model.TableNameUser)
	err := table1.Transaction(func(tx *gorm.DB) error {
		if err := tx.Where("uuid = ?", data.UserUUID).Where("is_del = ?", 0).First(&user).Error; err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		return err
	}
	mess := types.ReceiveMessages{}
	mess.Picture = user.Picture
	mess.UserUuid = user.UUID
	mess.NickName = user.NickName
	mess.CreateTime = time.Now().Format("2006-01-02 15:04:05")
	mess.Content = fmt.Sprintf("%s 加入了房间", user.NickName)
	resp := types.Response{
		Type: types.NotificationMessage,
		Body: response.Body{
			Code:    1,
			Data:    mess,
			Message: "成功",
		},
	}
	err = table.Transaction(func(tx *gorm.DB) error {
		if err := tx.Where("group_id = ? ", data.GroupID).Where("left_at IS NULL").Find(&members).Error; err != nil {
			return err
		}
		if len(members) != 0 {
			Accounts := make([]string, 0)
			// 获取账号的uuid 并切去重
			for _, member := range members {
				b := utils.Contains(Accounts, member.UserUUID)
				if !b {
					Accounts = append(Accounts, member.UserUUID)
				}

			}
			message, err := GetWebSocketMessage(&resp)
			if err != nil {
				sendErrByAccounts(Accounts, "", err)
			}
			SendByAccounts(Accounts, message)
		}
		return nil
	})
	if err != nil {
		return err
	}
	return nil
}
